<?php 
use Wdl\helper\VillageHelper;
use Wdl\helper\webHelper;
 
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
load()->func('tpl'); 
$uniacid = webHelper::bindUniacid(); 
$openid = $_W['openid'];
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';	
$template_name = sprintf("sshop/%s",  $operation);
 
$rd = !empty($_GPC['rd']) ? $_GPC['rd'] : 'index';	
$modelname = 'kbwdl_shequzufang';

/*门店管理列表*/
if($operation =='display'){
    /*删除门店成功*/
    if(!empty($_GPC['id'])){
       $params = array(':uniacid' => $uniacid,':id'=> intval($_GPC['id']) );
       pdo_query("delete  from  ".tablename('kbwdl_shop') ."  where uniacid=:uniacid and id=:id", $params);
       message("删除门店成功");
   }
   /*分页处理*/
 
   $condition = ' WHERE `uniacid` = :uniacid ';
   $params = array(':uniacid' => $uniacid );
   if (!empty($_GPC['keyword'])) {
           $condition .= ' AND `shopname` LIKE :shopname';
           $params[':shopname'] = '%' . trim($_GPC['keyword']) . '%';
   } 
   //获取列表数据
    $fd = webHelper::table_list_data('kbwdl_shop',$condition, $params);
    $total = $fd['total'];
    $list  = $fd['list'];
    $pager = $fd['pager'];

    /**新增门店 */
} elseif($operation =='add'){

    /**编辑的时候 */
    if(!empty($_GPC['id'])){
        $params = array(':uniacid' => $uniacid,':id'=> intval($_GPC['id']) );
        $item = pdo_fetch("select * from ".tablename('kbwdl_shop')." where uniacid=:uniacid and id=:id", $params);
    }
      $admin = get_user_permission($uniacid, $modelname);
 
 /* 设置 的属性*/  
} elseif($operation=='setproperty'){
    
    $id = intval($_GPC['id']);
    $type = $_GPC['type'];
    $data = intval($_GPC['data']);
    if (in_array($type, array('istop', 'isyou', 'ischeng'))) {
            $data = ($data==1?'0':'1');
            pdo_update("kbwdl_shop", array(  $type => $data), array("id" => $id, "uniacid" => $uniacid));
            die(json_encode(array("result" => 1, "data" => $data)));
    }
 
    die(json_encode(array("result" => 0)));
    /**
  * 提交 修改数据---
  */  
   
}elseif($operation=='addpost'){  

      if(checksubmit('submit')){
         $savedata = $_GPC['data'] ;
         /*封面图*/
         $savedata['thumb'] = $_GPC['thumb']; 
         $savedata['uniacid'] = $uniacid;
         $savedata['istop'] = isset($savedata['istop']) ? 1 :0;
         $savedata['isyou'] = isset($savedata['isyou']) ? 1 :0;
         $savedata['ischeng'] = isset($savedata['ischeng']) ? 1 :0;
         /*管理员姓名*/
         if($savedata['uid']>0){
            //一个店铺只能一对1个管理员
            pdo_update("kbwdl_shop", ['uid'=>0,'role_name'=>''],['uniacid' => $uniacid,'uid'=> $savedata['uid']]);
             $savedata['role_name'] = pdo_fetchcolumn("select username from ".tablename("users")." where uid='".$savedata['uid']."'");
         }
         /*保存数据*/
         if(intval($_GPC['id'])>0){
            /**/
            pdo_update("kbwdl_shop",$savedata, ['uniacid' => $uniacid,'id'=> intval($_GPC['id']) ] );
         }else{
             pdo_insert("kbwdl_shop", $savedata);
         }         
         message("操作成功",$this->createWebUrl('sshop' ), 'success');
     }
}

$submenus = [
    
    'display'=> ['name'=> '门店管理', 'do'=> 'sshop'],
    'add'=> ['name'=> '新增门店', 'do'=> 'sshop'],
];
/**
 * 查找到模块的管理员账号
 */
function get_user_permission($uniacid, $modelname){
    global $_W,$GPC; 
    $opersql = '';
    if($_W['user']['type']==3){    
        $opersql = " AND u.`uid` ='".$_W['uid']."' ";        
    }
    
    $role = pdo_fetchall("select u.username ,u.uid from ".tablename("users_permission").
            " as p  left join ".tablename("users")." as u on p.uid=u.uid where p.uniacid='$uniacid' and p.type='$modelname' $opersql");
    $admin = array();
    if(!empty($role)){
        foreach($role as $key=>$r){
            $admin[$r['uid']] = $r['username'];
        }
    }
    return $admin;
}
include $this->template('sshop/layout');
